﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef PORTMANAGER_H
#define PORTMANAGER_H

#include "PortBase.h"
namespace Base {
enum PORT_INDEX : int {
    MIN_PORT = 0,
    SERIAL_PORT = 0,
    TCP_PORT,
    MAX_PORT,
    DFU_PORT,
};
}

class PortManager {
public:
    void ClosePort() const;
    PortManager();
    virtual ~PortManager();
    bool InitPort(PortBaseInitStru &portBaseInit);
    ErrorSettingCom InitPortConfigure(UINT stopbits, UINT flowctrl, UINT baud,
                                      QSerialPort::Parity parity, UINT databits);
    BOOL ReInitPort();
    BOOL StartMonitoring();
    BOOL RestartMonitoring();
    BOOL StopMonitoring();

    void WriteToPort(LPCTSTR string);
    void WriteToPort(const char *string, int n);

    BOOL IsThreadAlive() const;

    void SetOneTimeReadByte(DWORD oneTimeReadByte);
    void SetPortIndex(unsigned int index);

    PortBase *GetSerialport();
    PortBase *GetTcpPort();
private:
    Base::PORT_INDEX portIndex = Base::SERIAL_PORT;
    PortBase *manager[Base::MAX_PORT];
};

#endif // PORTMANAGER_H
